package com.xyht.sca_s.student_manage_system.modules.rxRegistrationExamine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationExamine.entity.RxRegistrationExamineApplication;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationExamine.entity.RxRegistrationExamineBlackList;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationExamine.entity.req.RxRegistrationExamineApplicationAllowReq;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationExamine.entity.req.RxRegistrationExamineApplicationReq;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationExamine.entity.resp.RxRegistrationExamineApplicationResp;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationExamine.mapper.RxRegistrationExamineApplicationMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationExamine.mapper.RxRegistrationExamineBlackListMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationExamine.service.RxRegistrationExamineApplicationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.entity.resp.SmsSearchUserResp;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.rxRegistrationExamine.constant.ExamineAllowStatusConstant.*;

/**
 * <p>
 * 审批应用 黑名单学生入学申请 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-05-15
 */
@Service
public class RxRegistrationExamineApplicationServiceImpl extends ServiceImpl<RxRegistrationExamineApplicationMapper, RxRegistrationExamineApplication> implements RxRegistrationExamineApplicationService {
    @Resource
    private RxRegistrationExamineApplicationMapper rxRegistrationExamineApplicationMapper;
    @Resource
    private SmsUserMapper smsUserMapper;
    @Resource
    private RxRegistrationExamineBlackListMapper rxRegistrationExamineBlackListMapper;

    @Override
    public ResponseResult addStudentBlacklistApplication(RxRegistrationExamineApplicationReq applicationReq) {
        if (isNullOrEmpty(applicationReq.getStuName(), applicationReq.getStuIdCard(), applicationReq.getStuReason())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        List<RxRegistrationExamineBlackList> blackLists = rxRegistrationExamineBlackListMapper.selectList(new LambdaQueryWrapper<RxRegistrationExamineBlackList>()
                .eq(RxRegistrationExamineBlackList::getStuIdCard, applicationReq.getStuIdCard()));
        if (blackLists == null || blackLists.size() < 1){
            return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_BLACK_LIST_STUDENT_NOT_BLACK);
        }

        List<RxRegistrationExamineApplication> list = rxRegistrationExamineApplicationMapper.selectList(new LambdaQueryWrapper<RxRegistrationExamineApplication>()
                .eq(RxRegistrationExamineApplication::getStuIdCard, applicationReq.getStuIdCard())
                .eq(RxRegistrationExamineApplication::getAllowStatus, ALLOW_STATUS_UN_APPROVED));
        if (list != null && list.size() > 0) {
            return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_BLACK_LIST_STUDENT_APPLY_EXIST);
        }

        RxRegistrationExamineApplication addApplication = new RxRegistrationExamineApplication();
        BeanUtils.copyProperties(applicationReq, addApplication);
        addApplication.setAllowStatus(ALLOW_STATUS_UN_APPROVED);
        rxRegistrationExamineApplicationMapper.insert(addApplication);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getStudentBlacklistApplicationByStudent(String stu_id_card) {
        List<RxRegistrationExamineApplicationResp> list = rxRegistrationExamineApplicationMapper.selectList(new LambdaQueryWrapper<RxRegistrationExamineApplication>()
                        .eq(RxRegistrationExamineApplication::getStuIdCard, stu_id_card))
                .stream()
                .map(application -> {
                    RxRegistrationExamineApplicationResp resp = new RxRegistrationExamineApplicationResp();
                    BeanUtils.copyProperties(application, resp);
                    return resp;
                })
                .collect(Collectors.toList());
        return CommonResult.success(list);
    }

    @Override
    @Transactional
    public ResponseResult allowBlacklistApplication(RxRegistrationExamineApplicationAllowReq allowReq, String user_id) {
        if (isNullOrEmpty(allowReq.getId(), allowReq.getAllowStatus())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        if (allowReq.getAllowStatus() != ALLOW_STATUS_PASS && allowReq.getAllowStatus() != ALLOW_STATUS_DENY) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        RxRegistrationExamineApplication application = rxRegistrationExamineApplicationMapper.selectById(allowReq.getId());
        if (application == null) {
            return CommonResult.failed(CommonCodeEnum.RX_REGISTRATION_BLACK_LIST_STUDENT_APPLY_NOT_EXIST);
        }

        //修改审批状态
        RxRegistrationExamineApplication updateApplication = new RxRegistrationExamineApplication();
        updateApplication.setId(allowReq.getId());
        updateApplication.setAllowStatus(allowReq.getAllowStatus());
        updateApplication.setAllowOption(allowReq.getAllowOption());
        updateApplication.setAllowUserId(user_id);
        updateApplication.setAllowTime(new Date());
        rxRegistrationExamineApplicationMapper.updateById(updateApplication);

        //删除黑名单
        rxRegistrationExamineBlackListMapper.delete(new LambdaQueryWrapper<RxRegistrationExamineBlackList>()
                .eq(RxRegistrationExamineBlackList::getStuIdCard, application.getStuIdCard()));
        return CommonResult.success();
    }

    @Override
    public ResponseResult getStudentBlacklistApplicationList(String search_str, Integer status, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<RxRegistrationExamineApplication> queryWrapper = new LambdaQueryWrapper<>();
        if (!isNullOrEmpty(search_str)) {
            queryWrapper.and(wrapper ->
                    wrapper.like(RxRegistrationExamineApplication::getStuName, search_str)
                            .or()
                            .like(RxRegistrationExamineApplication::getStuIdCard, search_str));
        }
        if (!isNullOrEmpty(status)) {
            queryWrapper.eq(RxRegistrationExamineApplication::getAllowStatus, status);
        }

        queryWrapper.orderByDesc(RxRegistrationExamineApplication::getCreateTime);

        Page<RxRegistrationExamineApplication> page = new Page<>(pageNum, pageSize);
        rxRegistrationExamineApplicationMapper.selectPage(page, queryWrapper);
        List<RxRegistrationExamineApplicationResp> list = page.getRecords().stream()
                .map(application -> {
                    RxRegistrationExamineApplicationResp resp = new RxRegistrationExamineApplicationResp();
                    BeanUtils.copyProperties(application, resp);

                    if (!isNullOrEmpty(application.getAllowUserId())) {
                        SmsUser smsUser = smsUserMapper.selectById(application.getAllowUserId());
                        if (smsUser != null) {
                            SmsSearchUserResp userinfo = new SmsSearchUserResp();
                            BeanUtils.copyProperties(smsUser, userinfo);
                            resp.setUserinfo(userinfo);
                        }
                    }
                    return resp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(list, total);
    }
}
